//*********************************************************************
// Copyright (C) 2014 Hell Prototypes / www.hellprototypes.com
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
//********************************************************************
#include <avr/io.h>
#include <stdio.h>
#include "common.h"

void ram(void);
void flash_app(void);
void flash_bl(void);
void eeprom(void);
void eeprom_init(void);
void spi_flash(void);

//==================================

char c_main_menu_title[] 	= "Hell Watch Memory";

char c_eeprom[]		    = "EEPROM";
char c_eeprom_init[]		= "EEPROM Erase";
char c_spi_flash[]		    = "SPI Flash";
char c_ram[]		    	= "RAM";
char c_flash_app[]		    = "Application Flash";
char c_flash_bl[]		    = "Boot Loader Flash";

UI_listmenu_item_t c_main_menu_items[] = {
/*   Constent to display       callback function*/
    {c_ram, 	   	   		ram},
    {c_flash_app, 	   	   	flash_app},
    {c_flash_bl, 	   	   	flash_bl},//Boot Loader Flash Section was 
	{c_eeprom, 	    	eeprom},
	{c_eeprom_init, 		eeprom_init},
    {c_spi_flash, 	   		spi_flash},
	{NULL,                 	NULL}
};

UI_listmenu_t main_menu = {
	c_main_menu_title,
	c_main_menu_items,
	sizeof(c_main_menu_items)/sizeof(UI_listmenu_item_t)
};


//==================================
void memory_init(void)
{

}

uint8_t read_eeprom(uint32_t addr)
{
	return eeprom_read_byte ((const uint8_t *)((uint16_t)addr));
}

void eeprom(void)
{
	ui_hex_editor(0, 1024, read_eeprom);
}

void eeprom_init(void)
{
	uint16_t p;
	char str[16];

	for(p=0; p<1024; p++) {
		sprintf(str, "WR:%u/1024", p);
		draw_string(str, false, 0, 0);
		draw_end();
		//eeprom_write_byte ((uint8_t *)p, (uint8_t)(p&0xff));
		eeprom_write_byte((uint8_t *)p, 0xFF);
	}
	draw_string("Done...", false, 0, 0);
	draw_end();
	ms_delay(1000);
}

uint8_t read_spi_flash(uint32_t addr)
{
	uint8_t read;

	spi_read_data(addr, &read, 1);
	return read;
}

void spi_flash(void)
{
	ui_hex_editor(0, (uint32_t)1024*1024, read_spi_flash);
}

uint8_t read_ram(uint32_t addr)
{
	return *((uint8_t *)((uint16_t)addr));
}

void ram(void)
{
	ui_hex_editor(0x2000, 4096, read_ram);
}

uint8_t read_flash(uint32_t addr)
{
	return pgm_read_byte((uint16_t)addr);
}

void flash_app(void)
{
	ui_hex_editor(0x0000, (uint32_t)32*1024, read_flash);
}

void flash_bl(void)
{
	//Boot lock bit for the boot loader section was set to RWLOCK
	//The (E)LPM executing from the application section is not allowed 
	//to read from the boot loader section.
	//So this function will display all ZERO hex data
	ui_hex_editor((uint16_t)32*1024, (uint32_t)4*1024, read_flash);
}

